package com.example.qa_back.controller;

import com.example.qa_back.entity.Admin;
import com.example.qa_back.entity.Response;
import com.example.qa_back.service.AdminService;
import com.example.qa_back.utils.MD5;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 董帅
 * @date 2021/3/6 - 17:34
 */
@RestController
@RequestMapping("/api/admin")
public class AdminController {
    private final AdminService adminService;

    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }

    /*
    * 功能：登录
    * */
    @PostMapping("/login")
    public Response login(@RequestBody Admin admin) {
        Integer judgeNameExist = adminService.judgeNameExist(admin.getName());
        if (judgeNameExist == -1) {
            return new Response(0, "用户不存在", null);
        } else {
            String passwordById = adminService.getPasswordById(judgeNameExist);
            if (passwordById.equals(MD5.getMD5(admin.getPassword()))) {
                return new Response(1, "登录成功", null);
            } else {
                return new Response(2, "密码错误", null);
            }

        }
    }
}
